package admin

import (
	"go-amis-admin/amis"

	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Username   string     `gorm:"type:varchar(50);uniqueIndex;" amis:"label:用户名;validations:isRequired"`
	DeptID     uint       `amis:"label:部门;validations:isRequired;type:tree-select;source:/admin/user/getDeptTree" ignore:"list"`
	Department Department `gorm:"foreignKey:DeptID" amis:"label:部门;name:Department.Label" ignore:"edit,create"`
	RoleID     uint       `amis:"label:角色;validations:isRequired;type:select;source:/admin/role/getOptions" ignore:"list"`
	Role       Role       `gorm:"foreignKey:RoleID" amis:"label:角色;name:Role.Label" ignore:"edit,create"`
	Password   string     `gorm:"type:varchar(20)" amis:"label:密码" ignore:"list,edit"`
	Mobile     string     `gorm:"type:varchar(50);uniqueIndex" amis:"label:手机"`
	Email      string     `gorm:"type:varchar(50);uniqueIndex" amis:"label:邮箱;validations:isEmail"`
	Status     string     `gorm:"type:varchar(20)" amis:"label:状态;type:radios;source:/admin/dic/getDetailByType?code=UserStatus"`
}

func (u User) PK() uint {
	return u.ID

}

func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
	if u.Password != "" {
		u.Password, err = HashPassword(u.Password)
	}
	return
}

type Department struct {
	gorm.Model
	Label    string        `gorm:"type:varchar(20);uniqueIndex" amis:"label:名称;validations:isRequired"`
	ParentID uint          `amis:"label:上级部门;validations:isRequired"`
	Children []*Department `json:"children" gorm:"-"`
	Parent   *Department   `json:"parent" gorm:"-"`
}

func (d Department) GetID() uint {
	return d.ID
}

type DicType struct {
	gorm.Model
	Code   string `gorm:"type:varchar(20);uniqueIndex;" amis:"label:字典编码;validations:isRequired"`
	Label  string `gorm:"type:varchar(20)" amis:"label:字典名称;validations:isRequired"`
	Remark string `amis:"label:备注"`
}

func (dt DicType) GetID() uint {
	return dt.ID
}

type DicDetail struct {
	gorm.Model
	Code    string  `json:"code" amis:"label:类型编码;validations:isRequired" ignore:"edit,create"`
	TypeID  uint    `json:"typeId" amis:"label:类型;validations:isRequired;type:select;source:/admin/dic/getTypeOptions" ignore:"list"`
	DicType DicType `gorm:"foreignKey:TypeID" amis:"label:类型;name:DicType.Label" ignore:"edit,create"`
	Label   string  `json:"label" amis:"label:名称;validations:isRequired"`
	Val     string  `json:"value" amis:"label:值;validations:isRequired"`
}

func (d DicDetail) PK() uint {
	return d.ID
}

func (d *DicDetail) BeforeCreate(tx *gorm.DB) (err error) {
	var dicType DicType
	tx.Find(&dicType, d.TypeID)
	d.Code = dicType.Code
	return
}

type Role struct {
	gorm.Model
	Label  string `amis:"label:名称;validations:isRequired"`
	Remark string `amis:"label:备注"`
}

func (r Role) PK() uint {
	return r.ID
}

type LoginInfo struct {
	ID       uint
	Username string `json:"username"`
	RoleID   uint   `json:"roleId"`
	Password string `json:"-"`
	Token    string `json:"token"`
}

type EditModel[T GormModel] struct {
	Rows []T `json:"rows"`
}

type FilterOptions struct {
	KeywordFields []string
	FieldOptions  amis.MultiOptions
}

type ColumnsOptions struct {
	ListFields    []string
	ExcludeFields []string
	FieldOptions  amis.MultiOptions
}

type FormOptions struct {
	ExcludeFields []string
	FieldOptions  amis.MultiOptions
}

type Permission struct {
	Label string `json:"label"`
	Code  string `json:"name"`
}

type PermList []Permission

func PermDefault(moduleName string) PermList {
	ret := make(PermList, 0, 10)
	ret = append(ret,
		Permission{
			Label: "查看",
			Code:  moduleName + ".view",
		}, Permission{
			Label: "新增",
			Code:  moduleName + ".add",
		}, Permission{
			Label: "编辑",
			Code:  moduleName + ".edit",
		}, Permission{
			Label: "删除",
			Code:  moduleName + ".delete",
		})
	return ret
}
